﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BudgetApp.Data.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace BudgetApp.Data
{
    public class Seeder
    {
        public static void Seed(ApplicationDbContext db)
        {
            UserStore<ApplicationUser> userStore = new UserStore<ApplicationUser>(db); 
            UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(userStore);

            RoleStore<Role> roleStore = new RoleStore<Role>(db);
            RoleManager<Role> roleManager = new RoleManager<Role>(roleStore);

            if (!roleManager.RoleExists("General"))
            {
                var result = roleManager.Create(new Role
                {
                    Name = "General"
                });
            }

            if (!roleManager.RoleExists("Admin"))
            {
                var result = roleManager.Create(new Role
                {
                    Name = "Admin"
                });
            }

            ApplicationUser bob = userManager.FindByName("bob@gmail.com");

            if (bob == null)
            {
                bob = new ApplicationUser
                {
                    Email = "bob@gmail.com",
                    UserName = "bob@gmail.com",
                    FirstName = "Bob",
                    LastName = "Hope"
                };

                userManager.Create(bob, "Bhope1!");
                userManager.AddToRole(bob.Id, "Admin");

                bob = userManager.FindByName("bob@gmail.com");
            }

            ApplicationUser sally = userManager.FindByName("sally@gmail.com");

            if (sally == null)
            {
                sally = new ApplicationUser
                {
                    Email = "sally@gmail.com",
                    UserName = "sally@gmail.com",
                    FirstName = "Sally",
                    LastName = "Fields"
                };

                userManager.Create(sally, "Sfields1!");
                userManager.AddToRole(sally.Id, "General");

                sally = userManager.FindByName("sally@gmail.com");
            }

            ApplicationUser coral = userManager.FindByName("coral.morris33@gmail.com");

            if (coral == null)
            {
                coral = new ApplicationUser
                {
                    Email = "coral.morris33@gmail.com",
                    UserName = "coral.morris33@gmail.com",
                    FirstName = "Coral",
                    LastName = "Morris"
                };

                userManager.Create(coral, "Cmorris1!");
                userManager.AddToRole(coral.Id, "General");

                sally = userManager.FindByName("coral.morris33@gmail.com");
            }

            db.InputTypes.AddOrUpdate( i => new { i.Type}, 
                new InputType
                {
                    Type = "Expenses"
                },
                new InputType
                {
                    Type = "Income"
                },
                new InputType
                {
                    Type = "Bills"
                });

            db.Categories.AddOrUpdate( i => new { i.CategoryType},
                new Category
                {
                    CategoryType = "Housing"
                },
                new Category
                {
                    CategoryType = "PayCheck"
                },
                new Category
                {
                    CategoryType = "Utilities"
                },
                new Category
                {
                    CategoryType = "Entertainment"
                },
                new Category
                {
                    CategoryType = "Household Goods"
                });

            db.SubCategories.AddOrUpdate(i => new { i.SubCategoryType},
                new SubCategory
                {
                    SubCategoryType = "Rent"
                },
                new SubCategory
                {
                    SubCategoryType = "W-2"
                },
                new SubCategory
                {
                    SubCategoryType = "Water"
                },
                new SubCategory
                {
                    SubCategoryType = "Electricity"
                },
                new SubCategory
                {
                    SubCategoryType = "Movies"
                },
                new SubCategory
                {
                    SubCategoryType = "Groceries"
                });
        }

    }
}
